﻿/*************************************
 * Creator:SW-PeiYang
 * DateTime:2024/9/6 8:46:04
 * Description:<Description>
 * CopyRight:中广核研究院有限公司
 * ***********************************/

using Autofac;
using SunnyPaine.Autofac.Extension.Stereotype.Attributes;

namespace SunnyPaine.Autofac.Extension.Stereotype
{
    /// <summary>
    /// 当 <see cref="ContainerBuilder"/> 调用 Build() 方法后执行。
    /// </summary>
    public class BuildAfter : IBuildAfter
    {
        /// <summary>
        /// 注入被 <see cref="ResourceAttribute"/> 标记的属性。
        /// </summary>
        /// <param name="container">Autofac容器。</param>
        public void Execute(IContainer container)
        {
            ServiceLocator.Container = container;
            //foreach (IComponentRegistration registration in container.ComponentRegistry.Registrations)
            //{
            //    string name = registration.Activator.LimitType.Name;
            //    List<object> tmpInstances = new List<object>();
            //    Type[] interfaces = registration.Activator.LimitType.GetInterfaces();
            //    foreach (Type item in interfaces)
            //    {
            //        if (Injecter.UseInterfaceInject)
            //        {
            //            InjectAttribute injectAttribute = item.GetCustomAttribute<InjectAttribute>();
            //            if (injectAttribute == null)
            //            {
            //                continue;
            //            }
            //        }
            //        object tmp = null;
            //        if (container.IsRegistered(item))
            //        {
            //            tmp = container.Resolve(item);
            //        }
            //        else if (container.IsRegisteredWithName(name, item))
            //        {
            //            tmp = container.ResolveNamed(name, item);
            //        }
            //        if (tmp != null)
            //        {
            //            string typeName = tmp.GetType().FullName;
            //            if (typeName.StartsWith("Castle.Proxies") && typeName.EndsWith("Proxy"))
            //            {
            //                object originalTmp = Castle.DynamicProxy.ProxyUtil.GetUnproxiedInstance(tmp);
            //                tmpInstances.Add(originalTmp);
            //            }
            //            else
            //            {
            //                tmpInstances.Add(tmp);
            //            }
            //        }
            //    }

            //    //去重
            //    List<object> instances = tmpInstances.Distinct().ToList();

            //    PropertyInfo[] pis = registration.Activator.LimitType
            //        .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Default);
            //    foreach (PropertyInfo pi in pis)
            //    {
            //        ResourceAttribute resourceAttribute = pi.GetCustomAttribute<ResourceAttribute>();
            //        if (resourceAttribute != null)
            //        {
            //            if (resourceAttribute.Name != null)
            //            {
            //                object propertyInstance = container.ResolveNamed(resourceAttribute.Name, pi.PropertyType);
            //                foreach (object item in instances)
            //                {
            //                    pi.SetValue(item, propertyInstance);
            //                }
            //            }
            //            else
            //            {
            //                object propertyInstance = null;
            //                if (container.IsRegistered(pi.PropertyType))
            //                {
            //                    propertyInstance = container.Resolve(pi.PropertyType);
            //                }
            //                else if (container.IsRegisteredWithName(pi.Name, pi.PropertyType))
            //                {
            //                    propertyInstance = container.ResolveNamed(pi.Name, pi.PropertyType);
            //                }
            //                if (propertyInstance != null)
            //                {
            //                    object originalPropertyInstance = null;
            //                    string typeName = propertyInstance.GetType().FullName;
            //                    if (typeName.StartsWith("Castle.Proxies") && typeName.EndsWith("Proxy"))
            //                    {
            //                        originalPropertyInstance = Castle.DynamicProxy.ProxyUtil.GetUnproxiedInstance(propertyInstance);
            //                    }
            //                    else
            //                    {
            //                        originalPropertyInstance = propertyInstance;
            //                    }
            //                    foreach (object item in instances)
            //                    {
            //                        pi.SetValue(item, originalPropertyInstance);
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
        }
    }
}
